import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class CreateFrame extends JFrame{
    MyCanvas myCanvas;
    Snake snake;
    FoodCreate foods;
    int direction = 1;
    Ponit food;
    //    构造方法
    public CreateFrame(Snake snakes, Ponit food){
        this.snake = snakes;
        this.food = food;
        this.foods = new FoodCreate();
        myCanvas = new MyCanvas();
        JPanel jp = new JPanel();
        myCanvas.addKeyListener(new MyListener());
        jp.add(myCanvas);
        this.setTitle("贪吃蛇");
        this.setBounds(200,200,Setting.WIDTH + 30,Setting.HEIGHT + 60);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(jp);
        this.setVisible(true);
    }
    public void runGame(){
        System.out.println("游戏开始运行");
        while (true) {
            if (Setting.START) {
//            System.out.println("运行一次");
            Ponit tempPoint = snake.list.get(snake.list.size() - 1);
            for (int i = snake.list.size() - 1; i > 0; i--) {
//                System.out.println("身体移动" + i);
                   snake.list.get(i).setX(snake.list.get(i - 1).getX());
                   snake.list.get(i).setY(snake.list.get(i - 1).getY());
           }
            if (direction == 0) { // 上
                if(snake.list.get(0).getY() - 1 == food.getY() && snake.list.get(0).getX() == food.getX()){
                    snake.list.add(0, food);
                    food = foods.create(snake);
                } else {
                    snake.list.get(0).setY(snake.list.get(0).getY() - 1);
                }
            } else if (direction == 1) { //左
                if(snake.list.get(0).getY() == food.getY() && snake.list.get(0).getX()-1 == food.getX()) {
                    snake.list.add(0, food);
                    food = foods.create(snake);
                }else {
                    snake.list.get(0).setX(snake.list.get(0).getX() - 1);
                }
                }
            else if (direction == 2) { //下
                if(snake.list.get(0).getY()+1 == food.getY() && snake.list.get(0).getX() == food.getX()) {
                    snake.list.add(0, food);
                    food = foods.create(snake);
                } else {
                    snake.list.get(0).setY(snake.list.get(0).getY() + 1);
                }
            } else if (direction == 3) { // 右
                if(snake.list.get(0).getY() == food.getY() && snake.list.get(0).getX()+1 == food.getX()) {
                    snake.list.add(0, food);
                    food = foods.create(snake);
                } else {
                    snake.list.get(0).setX(snake.list.get(0).getX() + 1);
                }
            }
            try {
                Map.formatMap(snake, food);
                myCanvas.repaint();
                Thread.sleep(Setting.SPEED);
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println("出现错误");
            }
            }
        }
    }
    class MyListener implements KeyListener {

        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override
        public void keyPressed(KeyEvent e) {
            System.out.println("按下：" + e.getKeyCode());
            switch (e.getKeyCode()){
//                上
                case 38:
                    if (direction != 2)
                    direction = 0;
                    break;
//                    下
                case 40:
                    if (direction != 0)
                    direction = 2;
                    break;
//                    左
                case 37:
                    if (direction != 3)
                        direction = 1;
                    break;
//                    右
                case 39:
                    if (direction != 1)
                        direction = 3;
                    break;
                case 10:
                    Setting.START = true;
                    runGame();
                    break;
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {

        }
    }
    class MyCanvas extends Canvas{
        public MyCanvas(){
            setSize(Setting.WIDTH + 1,Setting.HEIGHT + 1);
            setBackground(Color.white);
        }
        public void paint(Graphics g){
//            System.out.println(Setting.WIDTH_NUM);
//            System.out.println(Setting.HEIGHT_NUM);
            int rectWidth = Setting.WIDTH/Setting.WIDTH_NUM; // 获取每个方块的宽度
            int rectHeight = Setting.HEIGHT/Setting.HEIGHT_NUM; //获取每个方块的高度
            // 循环画网格
            for (int i = 0; i <= Setting.WIDTH_NUM; i++) {
                for(int j = 0; j <= Setting.HEIGHT_NUM; j++) {
                    g.setColor(Setting.LINE_COLOR); // 设置线条颜色
                    g.drawLine(i*rectWidth,0,i*rectWidth,Setting.HEIGHT);
                    g.drawLine(0,j*rectHeight,Setting.WIDTH,j*rectHeight);
                    if (Map.map[i][j] == 1) {
//                        System.out.println("获取到" + Map.map[i][j]);
                        g.fillRect(i*rectWidth,j*rectHeight,rectWidth,rectHeight);
                    }
                    if (Map.map[i][j] == 2) {
//                        System.out.println("获取到" + Map.map[i][j]);
                        g.setColor(Color.red);
                        g.fillRect(i*rectWidth,j*rectHeight,rectWidth,rectHeight);
                    }
                    if (Map.map[i][j] == 3) {
//                        System.out.println("获取到" + Map.map[i][j]);
                        g.setColor(Color.green);
                        g.fillRect(i*rectWidth,j*rectHeight,rectWidth,rectHeight);
                    }
                }
            }
        }
    }

}
